Python:如何使用 boto 将文件上传到 S3 存储桶中的目录 | 您所在的位置:网站首页 › aws s3上传文件 › Python:如何使用 boto 将文件上传到 S3 存储桶中的目录 |
注意:此答案使用boto 。请参阅使用boto3的其他答案,该答案较新。 尝试这个... import boto import boto.s3 import sys from boto.s3.key import Key AWS_ACCESS_KEY_ID = '' AWS_SECRET_ACCESS_KEY = '' bucket_name = AWS_ACCESS_KEY_ID.lower() + '-dump' conn = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) bucket = conn.create_bucket(bucket_name, location=boto.s3.connection.Location.DEFAULT) testfile = "replace this with an actual filename" print 'Uploading %s to Amazon S3 bucket %s' % \ (testfile, bucket_name) def percent_cb(complete, total): sys.stdout.write('.') sys.stdout.flush() k = Key(bucket) k.key = 'my test file' k.set_contents_from_filename(testfile, cb=percent_cb, num_cb=10)[更新] 我不是 Python 专家,所以感谢您提醒我导入语句。另外,我不建议将凭据放在您自己的源代码中。如果您在 AWS 中运行它,请使用 IAM 凭据和实例配置文件 ( http://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_switch-role-ec2_instance-profiles.html ),并在中保持相同的行为你的开发/测试环境,使用来自 AdRoll ( https://github.com/AdRoll/hologram ) 的全息图 |
CopyRight 2018-2019 实验室设备网 版权所有 |